﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace tamlytre
{
    public partial class LoginAdmin : System.Web.UI.Page
    {
        DBTuVanDataContext db = new DBTuVanDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["IsLogin"] = 0;
            Session["TaiKhoan"] = null;
        }

        protected void btDangNhap_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                string userName = txtuser.Text.Trim();
                string pass = txtpass.Text.Trim();
                bool checktaikhoan = db.TB_Users.ToList().Exists(tk => tk.UserName == userName && tk.Password == pass);
                if (checktaikhoan == true)
                {
                    TB_User taikhoan = db.TB_Users.Where(tk => tk.UserName == userName).First();
                    Session["IsLogin"] = 1;
                    Session["TaiKhoan"] = taikhoan;
                    if (taikhoan.Role == 1 && taikhoan.Status == "HoatDong")
                    {
                        Response.Redirect("../Admin/Default.aspx");
                    }
                    else
                    {
                        lblMessage.Text = "Tài khoản chưa kích hoạt";
                    }
                }
                else
                    lblMessage.Text = "Sai tài khoản hoặc mật khẩu";
            }
        }

        protected void btThoat_Click(object sender, EventArgs e)
        {
            Response.Redirect("../Default.aspx");
        }
    }
}